package org.itzixi.controller;

import cn.hutool.core.bean.BeanUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.itzixi.base.BaseInfoProperties;
import org.itzixi.grace.result.GraceJSONResult;
import org.itzixi.grace.result.ResponseStatusEnum;
import org.itzixi.pojo.Users;
import org.itzixi.pojo.bo.ModifyUserBO;
import org.itzixi.pojo.vo.UsersVO;
import org.itzixi.service.UsersService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@RestController
@RequestMapping("userinfo")
public class UserController extends BaseInfoProperties {
    @Resource
    private UsersService usersService;
    @PostMapping("modify")
    public GraceJSONResult modify(@RequestBody ModifyUserBO userBO){
        //修改用户基本信息
        usersService.modifyUserInfo(userBO);
        //返回最新用户信息
        UsersVO usersVO = getUserInfo(userBO.getUserId(), true);
        return GraceJSONResult.ok(usersVO);
    }
    private UsersVO getUserInfo(String userId,boolean needToken){
        //查询获得最新用户信息
        Users latestUser= usersService.getById(userId);
        UsersVO usersVO=new UsersVO();
        BeanUtils.copyProperties(latestUser,usersVO);
        if(needToken){
            String uToken= TOKEN_USER_PREFIX+SYMBOL_DOT+ UUID.randomUUID();
            //只能在一台设备登录
//        redis.set(REDIS_USER_TOKEN+":"+userId,uToken);//设置分布式会话
            //可以在多台设备登录
            redis.set(REDIS_USER_TOKEN+":"+uToken,userId);
            usersVO.setUserToken(uToken);
        }
        return usersVO;
    }

    @PostMapping("get")
    public GraceJSONResult get(@RequestParam("userId") String userId){
        return GraceJSONResult.ok(getUserInfo(userId,false));
    }

    @PostMapping("updateFace")
    public GraceJSONResult updateFace(@RequestParam("userId") String userId,
                                      @RequestParam("face") String face){
        ModifyUserBO userBO=new ModifyUserBO();
        userBO.setUserId(userId);
        userBO.setFace(face);
        //修改用户基本信息
        usersService.modifyUserInfo(userBO);
        //返回最新用户信息
        UsersVO usersVO = getUserInfo(userBO.getUserId(), true);
        return GraceJSONResult.ok(usersVO);
    }

    @PostMapping("uploadFriendCircleBg")
    public GraceJSONResult uploadFriendCircleBg(@RequestParam("userId") String userId,
                                                @RequestParam("friendCircleBg") String friendCircleBg){
        ModifyUserBO userBO=new ModifyUserBO();
        userBO.setUserId(userId);
        userBO.setFriendCircleBg(friendCircleBg);
        //修改用户基本信息
        usersService.modifyUserInfo(userBO);
        //返回最新用户信息
        UsersVO usersVO = getUserInfo(userBO.getUserId(), true);
        return GraceJSONResult.ok(usersVO);
    }

    @PostMapping("uploadChatBg")
    public GraceJSONResult uploadChatBg(@RequestParam("userId") String userId,
                                        @RequestParam("chatBg") String chatBg){
        ModifyUserBO userBO=new ModifyUserBO();
        userBO.setUserId(userId);
        userBO.setChatBg(chatBg);
        //修改用户基本信息
        usersService.modifyUserInfo(userBO);
        //返回最新用户信息
        UsersVO usersVO = getUserInfo(userBO.getUserId(), true);
        return GraceJSONResult.ok(usersVO);
    }

//    private UsersVO commonDealUpdateUserInfo(@RequestParam("userId") String userId,
//                                             @RequestParam("chatBg") String chatBg){
//        ModifyUserBO userBO=new ModifyUserBO();
//        userBO.setUserId(userId);
//        userBO.setChatBg(chatBg);
//        //修改用户基本信息
//        usersService.modifyUserInfo(userBO);
//        //返回最新用户信息
//        UsersVO usersVO = getUserInfo(userBO.getUserId(), true);
//        return usersVO;
//    }

    @PostMapping("queryFriend")
    public GraceJSONResult queryFriend(String queryString, HttpServletRequest request){
        //查询条件为空
        if(StringUtils.isBlank(queryString)){
            return GraceJSONResult.error();
        }
        //查询
        Users friend = usersService.getByWechatNumOrMobile(queryString);
        //好友不存在
        if(friend==null){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FRIEND_NOT_EXIST_ERROR);
        }
        //如果好友是自己
        String myId = request.getHeader(HEADER_USER_ID);
        if(friend.getId().equals(myId)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.CAN_NOT_ADD_SELF_FRIEND_ERROR);
        }
        return GraceJSONResult.ok(friend);
    }
}
